package com.wanger.swc.config.dataInstance;

import cn.hutool.core.date.DateUtil;
import com.wanger.swc.config.dataInstance.model.DataInstanceType;
import com.wanger.swc.exception.BusEx;
import com.wanger.swc.model.tips.TipInfo;
import com.wanger.swc.util.validation.MyVerifyUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: wanger
 * @Date: 2025/3/11 9:16
 * @Description:
 */
@Slf4j
@Component
@RequiredArgsConstructor
@Order(Ordered.HIGHEST_PRECEDENCE)//最高优先级
public class DataInstanceDispatcher implements ApplicationRunner {
    private final ApplicationContext applicationContext;

    /**
     * 启动时验证DataInstanceService实现类合法性
     *
     * @param args incoming application arguments
     */
    @Override
    public void run(ApplicationArguments args) {
        log.info("开始验证DataInstanceService实现类合法性！");
        long l = System.currentTimeMillis();
        verifyValidity();
        log.info("DataInstanceService实现类合法性验证完成，用时【{}】！", DateUtil.formatBetween(System.currentTimeMillis() - l));
    }

    /**
     * 验证合法性，验证内容：1.getType都不能为空，2.getType都不能重复
     */
    private void verifyValidity() {
        Collection<DataInstanceService> values = applicationContext.getBeansOfType(DataInstanceService.class).values();
        Map<DataInstanceType, DataInstanceService> map = new HashMap<>();
        for (DataInstanceService value : values) {
            DataInstanceType type = MyVerifyUtil.nullVerify(value::getType, "实现类【{}】的getType不能为空！", value.getClass().getName());
            if (map.get(type) != null) {
                BusEx.throwEx("在【{}】实现类与【{}】实现类的getType()配置存在重复！", map.get(type).getClass().getName(), value.getClass().getName());
            } else map.put(type, value);
        }
    }

    /**
     * 选举实现类
     *
     * @param type 目标类型
     * @return 实现类
     */
    public DataInstanceService getService(String type) {
        DataInstanceType byType = DataInstanceType.getByType(type);
        Collection<DataInstanceService> values = applicationContext.getBeansOfType(DataInstanceService.class).values();
        for (DataInstanceService value : values) {
            if (value.getType() == byType) {
                return value;
            }
        }
        throw BusEx.create("不支持的类型【{}】解析！", type);
    }

    /**
     * 获取实现类列表ui提示
     *
     * @return 实现类列表ui提示
     */
    public List<TipInfo> uiSupportType() {
        return applicationContext.getBeansOfType(DataInstanceService.class)
                .values()
                .stream()
                .map(e -> new TipInfo(e.getType().getType(), e.getType().getDesc()))
                .collect(Collectors.toList());
    }

    /**
     * 将原始数据对象转换为json数据对象
     *
     * @param contentType 类型
     * @param content     原始内容
     * @return 转换后的json数据对象
     */
    public Object getJsonData(String contentType, String content) {
        return getService(contentType).getJsonData(content);
    }
}
